home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr48 / pasclern.zip / CHAP8.TXT < prev    next >
Text File  |  1993-04-01  |  13KB  |  257 lines

  1.                 CHAPTER 8 - Scalars, subranges, and sets.
  2.  
  3.  
  4.                               PASCAL SCALARS
  5.  
  6.             A scalar,  also called an enumerated type,  is a list of 
  7.         values  which a variable of that type may assume.   Look  at 
  8.         the  Pascal program ENTYPES for an example of some  scalars.  
  9.         The  first  type declaration defines "days" as being a  type 
  10.         which can take on any one of seven  values.   Since,  within 
  11.         the  VAR declaration,  "day" is assigned the type of "days", 
  12.         then  "day" is a variable which can assume any one of  seven 
  13.         different values.   Moreover "day" can be assigned the value 
  14.         "mon",  or  "tue",  etc.   This makes the program easier  to 
  15.         follow and understand.  Internally, Pascal does not actually 
  16.         assign the value "mon" to the variable "day", but it uses an 
  17.         integer  representation  for each of  the  names.   This  is 
  18.         important to understand because you need to realize that you 
  19.         cannot print out "mon",  "tue",  etc., but can only use them 
  20.         for indexing control statements.
  21.  
  22.             The   second   line  of  the  type  definition   defines 
  23.         "time_of_day"  as another scalar which can have any of  four 
  24.         different values,  namely those listed.  The variable "time" 
  25.         can only be assigned one of four values since it is  defined 
  26.         as  the  type "time_of_day".   It should be clear that  even 
  27.         though   it  can  be  "morning",   it  cannot  be   assigned 
  28.         "morning_time"  or  any other variant spelling  of  morning, 
  29.         since  it  is simply another identifier which must  have  an 
  30.         exact spelling to be understood by the compiler.
  31.  
  32.             Several  real  variables  are defined  to  allow  us  to 
  33.         demonstrate the use of the scalar variables.   After writing 
  34.         a header for our output,  the real variables are initialized 
  35.         to  some values that are probably not real life values,  but 
  36.         will serve to illustrate the scalar variable.
  37.  
  38.                         A BIG SCALAR VARIABLE LOOP
  39.  
  40.             The  remainder  of the program is one large  loop  being 
  41.         controlled  by the variable "day" as it goes through all  of 
  42.         its values,  one at a time.   Note that the loop could  have 
  43.         gone  from  "tue" to "sat" or whatever portion of the  range 
  44.         desired, it does not have to go through all of the values of 
  45.         "day".  Using "day" as the case variable, the name of one of 
  46.         the days of the week is written out each time we go  through 
  47.         the  loop.   Another  loop controlled by "time" is  executed 
  48.         four  times,  once for each value of "time".   The two  CASE 
  49.         statements  within the inner loop are used to calculate  the 
  50.         total pay rate for each time period and each day.   The data 
  51.         is  formatted carefully to make a nice looking table of  pay 
  52.         rates as a function of "time" and "day".
  53.  
  54.  
  55.  
  56.  
  57.                                  Page 35
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                 CHAPTER 8 - Scalars, subranges, and sets.
  68.  
  69.  
  70.             Take  careful  notice  of  the  fact  that  the   scalar 
  71.         variables never entered into the calculations, and they were 
  72.         not printed out.  They were only used to control the flow of 
  73.         logic. It was much neater than trying to remember that "mon" 
  74.         is represented by a 0, "tue" is represented by a 1, etc.  In 
  75.         fact, those numbers are used for the internal representation 
  76.         of  the scalars but we can relax and let Pascal worry  about 
  77.         the internal representation of our scalars.
  78.  
  79.                         LETS LOOK AT SOME SUBRANGES
  80.  
  81.             Examine   the   program  SUBRANGE  for  an  example   of 
  82.         subranges.   It  may be expedient to define  some  variables 
  83.         that  only  cover a part of the full range as defined  in  a 
  84.         scalar  type.   Notice that "days" is declared a scalar type 
  85.         as in the last program,  and "work" is declared a type  with 
  86.         an  even  more restricted range.   In the  VAR  declaration, 
  87.         "day"  is once again defined as the days of the week and can 
  88.         be  assigned any of the days by the program.   The  variable 
  89.         "workday",  however,  is assigned the type "work",  and  can 
  90.         only  be  assigned  the days "mon"  through  "fri".   If  an 
  91.         attempt  is  made  to assign "workday" the  value  "sat",  a 
  92.         runtime  error  will  be  generated.   A  carefully  written 
  93.         program  will  never  attempt  that,  and  it  would  be  an 
  94.         indication  that something is wrong with either the  program 
  95.         or the data.   This is one of the advantages of Pascal  over 
  96.         older languages.
  97.  
  98.             Further examination will reveal that "index" is assigned 
  99.         the  range of integers from 1 through 12.   During execution 
  100.         of the program,  if an attempt is made to assign "index" any 
  101.         value  outside  of  that range,  a run time  error  will  be 
  102.         generated.   Suppose  the variable "index" was  intended  to 
  103.         refer  to  your  employees,  and you have only  12.   If  an 
  104.         attempt was made to refer to employee number 27, or employee 
  105.         number -8,  there is clearly an error somewhere in the  data 
  106.         and  you  would want to stop running the payroll to fix  the 
  107.         problem.  Pascal would have saved you a lot of grief.
  108.  
  109.                    SOME STATEMENTS WITH ERRORS IN THEM.
  110.  
  111.             In  order to have a program that would  compile  without 
  112.         errors,  and  yet  show some errors,  the first part of  the 
  113.         program  is  not really a part of the program  since  it  is 
  114.         within a comment area.  This is a trick to remember when you 
  115.         are debugging a program, a troublesome part can be commented 
  116.         out  until  you are ready to include it in  the  rest.   The 
  117.         errors are self explanatory.
  118.  
  119.             There  are  seven assignment statements as  examples  of 
  120.         subrange  variable use.   Notice that the variable "day" can 
  121.  
  122.  
  123.                                  Page 36
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                 CHAPTER 8 - Scalars, subranges, and sets.
  134.  
  135.  
  136.         always  be  assigned  the  value  of  either  "workday"   or 
  137.         "weekend",  but  the  reverse is not true because "day"  can 
  138.         assume values that would be illegal for the others.
  139.  
  140.                         THREE VERY USEFUL FUNCTIONS
  141.  
  142.             The last section of the example program demonstrates the 
  143.         use  of three very important functions when  using  scalars.  
  144.         The  first is the "succ" function that returns the value  of 
  145.         the successor to the scalar used as an argument, that is the 
  146.         next value.   If the argument is the last value,  a run time 
  147.         error  is  generated.   The  next  function  is  the  "pred" 
  148.         function that returns the predecessor to the argument of the 
  149.         function.   Finally  the  "ord" function which  returns  the 
  150.         ordinal  value of the scalar.   All scalars have an internal 
  151.         representation starting at 0 and increasing by one until the 
  152.         end is reached.   In our example program, "ord(day)" is 5 if 
  153.         "day"  has been assigned "sat",  but "ord(weekend)" is 0  if 
  154.         "weekend" has been assigned "sat".   As you gain  experience 
  155.         in programming with scalars and subranges,  you will realize 
  156.         the value of these three functions.
  157.  
  158.             A  few more thoughts about subranges are in order before 
  159.         we go on to another topic.   A subrange is always defined by 
  160.         two  predefined  constants,  and  is always  defined  in  an 
  161.         ascending order.   A variable defined as a subrange type  is 
  162.         actually  a  variable defined with a restricted  range,  and 
  163.         should  be  used as often as possible in  order  to  prevent 
  164.         garbage  data.   There are actually very few variables  ever 
  165.         used  that cannot be restricted by some amount.   The limits 
  166.         may give a hint at what the program is doing and can help in 
  167.         understanding  the program operation.   Subrange  types  can 
  168.         only be constructed using the simple types,  INTEGER,  CHAR, 
  169.         or scalar.
  170.  
  171.                                    SETS
  172.  
  173.             Now for a new topic, sets.  Examining the example Pascal 
  174.         program  SETS will reveal some sets.   A scalar variable  is 
  175.         defined first, in this case the scalar type named "goodies".  
  176.         A  set  is  then  defined with the  reserved  words  SET  OF 
  177.         followed by a predefined scalar type.  Several variables are 
  178.         defined   as   sets  of  "treat",   after  which  they   can 
  179.         individually be assigned portions of the entire set.
  180.  
  181.             Consider  the variable "ice_cream_cone" which  has  been 
  182.         defined as a set of type "treat".  This variable is composed 
  183.         of as many elements of "goodies" as we care to assign to it.  
  184.         In   the  program,   we  define  it  as  being  composed  of 
  185.         "ice_cream",   and  "cone".   The  set  "ice_cream_cone"  is 
  186.         therefore composed of two elements,  and it has no numerical 
  187.  
  188.  
  189.                                  Page 37
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                 CHAPTER 8 - Scalars, subranges, and sets.
  200.  
  201.  
  202.         or   alphabetic   value  as  most  other   variables   have.  
  203.         Continuing  in the program,  you will see 4  more  delicious 
  204.         deserts  defined as sets of their components.   Notice  that 
  205.         the banana split is first defined as a range of terms,  then 
  206.         another  term is added to the group.   All five are combined 
  207.         in  the set named "mixed",  then "mixed" is subtracted  from 
  208.         the entire set of values to form the set of ingredients that 
  209.         are not used in any of the deserts.  Each ingredient is then 
  210.         checked  to see if it is IN the set of  unused  ingredients, 
  211.         and printed out if it is.  Running the program will reveal a 
  212.         list of unused elements.
  213.  
  214.             In this example,  better programming practice would have 
  215.         dictated   defining   a  new   variable,   possibly   called 
  216.         "remaining" for the ingredients unused.  It was desirable to 
  217.         illustrate  that "mixed" could be assigned a value based  on 
  218.         subtracting itself from the entire set, so the poor variable 
  219.         name was used.
  220.  
  221.             This  example  resulted  in some  nonsense  results  but 
  222.         hopefully it led your thinking toward the fact that sets can 
  223.         be  used for inventory control,  possibly a parts allocation 
  224.         scheme, or some other useful system.
  225.  
  226.                             SEARCHING WITH SETS
  227.  
  228.             The Pascal program FINDCHRS is more useful than the last 
  229.         one.  In it we start with a short sentence and search it for 
  230.         all lower case alphabetic letters and write a list of  those 
  231.         used.  Since we are using a portion of the complete range of 
  232.         CHAR,  we do not need to define a scalar before defining the 
  233.         set,  we  can define the set using the range 'a'..'z'.   The 
  234.         set  "data_set" is assigned the value of no elements in  the 
  235.         first statement of the program,  and the print string, named 
  236.         "print_group",  is set to blank in the next.   The  variable 
  237.         "storage" is assigned the sentence to search, and the search 
  238.         loop  is  begun.   Each time through the loop,  one  of  the 
  239.         characters  is  checked.   It is either declared as  a  non-
  240.         lower-case character,  is a repeat of one already found,  or 
  241.         is a new one added to the list.
  242.  
  243.             You  are  left to decipher the details of  the  program, 
  244.         which  should be no problem since there is nothing new here.  
  245.         Run  the  program and observe how the list  grows  with  new 
  246.         letters as the sentence is scanned.
  247.  
  248.                            PROGRAMMING EXERCISE
  249.  
  250.         1. Modify FINDCHRS to search for upper-case letters.
  251.  
  252.  
  253.  
  254.  
  255.                                  Page 38
  256.  
  257.